using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CrazyDogToDo.Service.Services;
using CrazyToDo.Web.Models;
using Microsoft.AspNetCore.Http;

namespace CrazyToDo.Web.Controllers
{
    public class HomeController : Controller
    {       
        private readonly MatterService _matterService = new MatterService();
        /// <summary>
        /// 展示所有事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowMatter(int id)
        {
            var matter = _matterService.FindMatterById(id);

            MatterViewModel matterViewModel = new MatterViewModel()
            {
                Email = matter.Email,
                Content = matter.Content,
                Remark = matter.Remark,
                CreateTime = matter.CreateTime,
                OverdueTime = matter.OverdueTime,
                State = matter.State
            };

            return View("ShowMatter", matterViewModel);
        }
        /// <summary>
        /// 进入主界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            var matters = _matterService.GetInventoryMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View("Home", matterViewModels);
        }
        /// <summary>
        /// 添加事项
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public IActionResult Add(string content, string remark, string overdueTime)
        {
            _matterService.CreateMatter(HttpContext.Session.GetString("user"), content, remark, overdueTime);
            return RedirectToAction("Index");
        }
        [HttpGet]
        /// <summary>
        ///改变事项状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ChangeState(int id)
        {
            _matterService.ChangeMatterState(HttpContext.Session.GetString("user"), id);
            var matters = _matterService.GetInventoryMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View(matterViewModels);
        }
        [HttpGet]
        /// <summary>
        /// 改变参与的活动状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ChangeActivityState(int id)
        {
            _matterService.ChangeMatterState(HttpContext.Session.GetString("user"), id);
            var matters = _matterService.GetActivityMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View("MyActivity", matterViewModels);
        }
        /// <summary>
        /// 显示我参与的活动
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult MyActivity()
        {
            var matters = _matterService.GetActivityMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View(matterViewModels);
        }
        [HttpGet]
        /// <summary>
        /// 删除我的一天下的事项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult DeleteMatter(int id)
        {
            _matterService.DeleteMatter(id);
            var matters = _matterService.GetInventoryMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View("ChangeState",matterViewModels);
        }

        public IActionResult DeleteActivityMatter(int id)
        {
            _matterService.DeleteMatter(id);
            var matters = _matterService.GetActivityMatter(HttpContext.Session.GetString("user"));
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View("MyActivity", matterViewModels);
        }
    }
}